#include "light.h"

void Light::init(void)
{
	Color(1.0f,1.0f,1.0f,1.0f);
}

Light::Light(void)
{
	glEnable(GL_LIGHTING);
	bool no_create=false;
	int i;
	for(i=0;i<9;i++)
	{
		if(i==8)no_create=true;
		else
		{
			if(glIsEnabled(GL_LIGHT0+i)==false)
			{
				break;
			}
		}

	}
	if(!no_create)
	{
		lnum=i;
		glEnable(GL_LIGHT0+i);
		init();
	}
}

Light::Light(unsigned char Type_)
{
	init();
}

Light::~Light(void)
{
	glDisable(GL_LIGHT0+lnum);
	bool offline=true;
	for(int i=0;i<8;i++)
	{
		if(glIsEnabled(GL_LIGHT0+i))
		offline=false;
	}	
	if(offline){glDisable(GL_LIGHTING);}
}

void Light::Color(float r,float g,float b,float a=1.0f)
{
	//AmbientColor(r*0.33f,g*0.33f,b*0.33f,a);
	DiffuseColor(r,g,b,a);
	SpecColor	(r*0.8f,g*0.8f,b*0.8f,a);
}

void Light::SpecColor(float r,float g,float b,float a=1.0f)
{
	spec[0]=r;
	spec[1]=g;
	spec[2]=b;
	spec[3]=a;

}

void Light::AmbientColor(float r,float g,float b,float a=1.0f)
{
	ambient[0]=r;
	ambient[1]=g;
	ambient[2]=b;
	ambient[3]=a;
}

void Light::DiffuseColor(float r,float g,float b,float a=1.0f)
{
	diffuse[0]=r;
	diffuse[1]=g;
	diffuse[2]=b;
	diffuse[3]=a;
}

void Light::Draw(void)
{
	GLfloat mat[16];
	glGetFloatv( GL_MODELVIEW_MATRIX, mat );
	Vector4D lightpos(-100,50,10,1);
	lightpos = cams::modelview * lightpos;

	//debug::add(mat[12]);
	glLightfv(lnum,GL_AMBIENT,ambient);
	glLightfv(lnum,GL_DIFFUSE,diffuse);
	glLightfv(lnum,GL_SPECULAR,spec);
	glLightfv(lnum,GL_POSITION,&lightpos.x);
	glEnable(GL_LIGHTING);

	glEnable(GL_COLOR_MATERIAL);
}